﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTcms.Common;

using DTcms.Model;
using System.Data;
using Newtonsoft.Json;
namespace DTcms.JOB.users.command
{
    public  class CheckUserGroupCommand
    {
        public async Task<string>  run() {
            try {
                Utils.WriteTextLog("CheckUserGroup", "检查用户VIP状态开始", DateTime.Now);
                //创建表记录重置会员级别的所有会员记录
                DTcms.JOB.users.DAL.user_expired_log logdal = new DAL.user_expired_log();
                DTcms.JOB.users.DAL.user2company udal = new DAL.user2company();
                string abuser = "";
                abuser = " and mobile not in ('15666612389','15624451111')";
                DataSet ds= udal.GetList(0, "upgrade_time is not null and group_id > 1 and DateDiff(dd, DateAdd(yy, 1, upgrade_time), getdate()) > 3 "+ abuser, "upgrade_time desc");
                Utils.WriteTextLog("CheckUserGroup", "检查用户VIP状态操作1-查询已到期会员总数："+ds.Tables[0].Rows.Count, DateTime.Now);
                if (ds.Tables.Count > 0) {
                    foreach (DataRow dr in ds.Tables[0].Rows) {
                        //修改会员的级别为1
                        Model.user_expired_log log = new Model.user_expired_log();
                        log.user_id = int.Parse(dr["id"] + "");
                        log.remark = "会员过期自动JOB,";
                        log.add_time = DateTime.Now;
                        logdal.Add(log);
                        udal.UpdateField(int.Parse(dr["id"] + ""), "group_id=1");
                        udal.UpdateFields(int.Parse(dr["id"] + ""), "group_id=1,sort_id=0");
                    }
                }
                Utils.WriteTextLog("CheckUserGroup", "检查用户VIP状态结束", DateTime.Now);
                return "success";
            }
            catch(Exception ex) {
                Utils.WriteTextLog("CheckUserGroup", "检查用户VIP状态error：" + JsonConvert.SerializeObject(ex), DateTime.Now);
                return ex.Message;
            }
        }
    }
}
